﻿package mortal.game.view.market.qiugou
{
    import Message.DB.Tables.*;
    import com.mui.controls.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import fl.data.*;
    import flash.events.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.pageSelect.*;
    import mortal.game.view.market.*;
    import mortal.game.view.market.sale.*;
    import mortal.mvc.core.*;

    public class MktQiugouRtPanel extends GSprite
    {
        private var _bgTitle1:ScaleBitmap;
        private var _title1:GBitmap;
        private var _list:GTileList;
        private var _pageSelect:PageSelecter;
        private var _bgTitle2:ScaleBitmap;
        private var _title2:GBitmap;
        private var _line:ScaleBitmap;
        private var _txtSearch:TextInputList;
        private var _btnMagnifier:GLoadingButton;
        private var _txtType1:GTextFiled;
        private var _comboType1:GComboBox;
        private var _comboColor:GComboBox;
        private var _comboType2:GComboBox;
        private var _comboCareer:GComboBox;
        private var _comboLevel:GComboBox;
        private var _btnSearch:GButton;
        private var _btnReset:GButton;
        private var _moneys:MktMoneys;
        private var _searchCondition:Object;
        private var _selectSp:ScaleBitmap;
        private var _selectItemInfo:ItemInfo;
        private var _selectPage:int;
        private var _itemArr:Array;

        public function MktQiugouRtPanel()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            var _loc_5:Object = null;
            super.createDisposedChildrenImpl();
            pushUIToDisposeVec(UIFactory.bg(0, 0, 328, 126, this));
            this._bgTitle1 = UIFactory.bg(0, 0, 328, 25, this, "RegionTitleBg");
            this._title1 = UIFactory.bitmap(ImagesConst.market4, 0, 0, this);
            this._title1.x = (this._bgTitle1.width - this._title1.width) / 2;
            this._title1.y = this._bgTitle1.y + (this._bgTitle1.height - this._title1.height) / 2;
            this._selectSp = UIFactory.bg(0, 0, 45, 45, this._list, ImagesConst.selectFilter);
            this._selectSp.visible = false;
            this._pageSelect = UIFactory.pageSelecter(93, 116, this, PageSelecter.CompleteMode);
            this._pageSelect.setbgStlye(ImagesConst.ComboBg, new GTextFormat());
            this._pageSelect.maxPage = 1;
            this._pageSelect.pageTextBoxSize = 36;
            this._pageSelect.configEventListener(Event.CHANGE, this.onPageChange);
            this._pageSelect.visible = false;
            this._list = UIFactory.tileList(4, 30, 330, 90, this);
            this._list.useFixed = false;
            this._list.rowHeight = 44;
            this._list.columnWidth = 44;
            this._list.horizontalGap = 0;
            this._list.verticalGap = 0;
            this._list.setStyle("cellRenderer", MktBagItemRenderer);
            var _loc_1:Array = [];
            var _loc_2:int = 0;
            while (_loc_2 < 2 * this._list.columnCount)
            {
                
                _loc_1.push(new Object());
                _loc_2++;
            }
            this._list.dataProvider = new DataProvider(_loc_1);
            pushUIToDisposeVec(UIFactory.bg(0, 140, 328, 198, this));
            this._bgTitle2 = UIFactory.bg(0, 140, 328, 25, this, "RegionTitleBg");
            this._title2 = UIFactory.bitmap(ImagesConst.market3, 0, 0, this);
            this._title2.x = (this._bgTitle2.width - this._title2.width) / 2;
            this._title2.y = this._bgTitle2.y + (this._bgTitle2.height - this._title2.height) / 2;
            this._line = UIFactory.bg(0, 200, 328, 1, this, ImagesConst.SplitLine);
            this._txtSearch = UIFactory.getUICompoment(TextInputList, 109, 170, this);
            this._txtSearch._textInput.textField.width = this._txtSearch._textInput.textField.width - 25;
            this._txtSearch.init(this.onSearchTxtChange);
            this._btnMagnifier = UIFactory.gLoadingButton(ResFileConst.SearchBtn, this._txtSearch.x + this._txtSearch._textInput.width - 21, this._txtSearch.y + 2, 19, 19, this);
            this._comboType1 = UIFactory.gComboBox(45, 213, 100, 20, null, this);
            var _loc_3:* = MktModConfig.dpItemType1.clone();
            var _loc_4:int = 0;
            while (_loc_4 < _loc_3.length)
            {
                
                _loc_5 = _loc_3.getItemAt(_loc_4);
                if (_loc_5.label == "金币&钻石")
                {
                    _loc_3.removeItemAt(_loc_4);
                }
                _loc_4++;
            }
            this._comboType1.dataProvider = _loc_3;
            this._comboType1.selectedIndex = 0;
            this._comboType2 = UIFactory.gComboBox(45, 242, 100, 20, null, this);
            this.resetComboType2();
            this._comboColor = UIFactory.gComboBox(200, 213, 100, 20, null, this);
            this._comboColor.dataProvider = MktModConfig.dpColor.clone();
            this._comboCareer = UIFactory.gComboBox(200, 242, 100, 20, null, this);
            this._comboCareer.dataProvider = MktModConfig.dpCareer.clone();
            this.autoSelectMyCareer();
            this._comboLevel = UIFactory.gComboBox(45, 271, 100, 20, null, this);
            this._comboLevel.dataProvider = MktModConfig.dpLevel.clone();
            pushUIToDisposeVec(UIFactory.textField("精确查找：", 45, 170, 68, 22, this, GlobalStyle.textFormatAnjin));
            pushUIToDisposeVec(UIFactory.textField("类型：", 7, 213, 40, 22, this, GlobalStyle.textFormatAnjin));
            pushUIToDisposeVec(UIFactory.textField("颜色：", 163, 213, 40, 22, this, GlobalStyle.textFormatAnjin));
            pushUIToDisposeVec(UIFactory.textField("种类：", 7, 242, 40, 22, this, GlobalStyle.textFormatAnjin));
            pushUIToDisposeVec(UIFactory.textField("职业：", 163, 242, 40, 22, this, GlobalStyle.textFormatAnjin));
            pushUIToDisposeVec(UIFactory.textField("等级：", 7, 271, 40, 22, this, GlobalStyle.textFormatAnjin));
            this._btnSearch = UIFactory.gButton("搜索", 90, 304, 55, 22, this);
            this._btnReset = UIFactory.gButton("重置", 168, 304, 55, 22, this);
            this._moneys = UICompomentPool.getUICompoment(MktMoneys);
            this._moneys.createDisposedChildren();
            this._moneys.x = 5;
            this._moneys.y = 355;
            addChild(this._moneys);
            Dispatcher.addEventListener(EventName.MarketRemoveSeekItem, this.onRemoveSeekItem);
            this._comboType1.configEventListener(Event.CHANGE, this.onComboboxChange);
            this._comboType2.configEventListener(Event.CHANGE, this.onComboboxChange);
            this._comboLevel.configEventListener(Event.CHANGE, this.onComboboxChange);
            this._comboColor.configEventListener(Event.CHANGE, this.onComboboxChange);
            this._comboCareer.configEventListener(Event.CHANGE, this.onComboboxChange);
            this._btnMagnifier.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._btnSearch.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._btnReset.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._list.configEventListener(Event.CHANGE, this.listSelectChange);
            return;
        }// end function

        private function onRemoveSeekItem(event:DataEvent) : void
        {
            this._selectItemInfo = null;
            this._list.selectedIndex = -1;
            this.listSelectChange(null);
            return;
        }// end function

        private function listSelectChange(event:Event) : void
        {
            var _loc_3:MktBagItemRenderer = null;
            if (this._list.selectedIndex == -1)
            {
                this.setSelectSpPos(null);
                return;
            }
            var _loc_2:* = this._list.selectedItem;
            if (_loc_2 is ItemInfo)
            {
                _loc_3 = this._list.itemToCellRenderer(_loc_2) as MktBagItemRenderer;
                this._selectItemInfo = _loc_2 as ItemInfo;
                this._selectPage = this._pageSelect.currentPage;
                Dispatcher.dispatchEvent(new DataEvent(EventName.MarketPushSeekItem, _loc_2));
            }
            this.setSelectSpPos(_loc_3);
            return;
        }// end function

        private function setSelectSpPos(param1:MktBagItemRenderer) : void
        {
            return;
        }// end function

        private function autoSelectMyCareer() : void
        {
            var _loc_3:Object = null;
            var _loc_1:* = this._comboCareer.dataProvider;
            var _loc_2:int = 0;
            while (_loc_2 < _loc_1.length)
            {
                
                _loc_3 = _loc_1.getItemAt(_loc_2);
                if (_loc_3.career == Cache.instance.role.entityInfo.career)
                {
                    this._comboCareer.selectedIndex = _loc_2;
                    return;
                }
                _loc_2++;
            }
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            if (event.currentTarget == this._btnMagnifier)
            {
                if (this._txtSearch._textInput.text.length < 2)
                {
                    MsgManager.showRollTipsMsg("请输入两个以上关键字");
                    return;
                }
                this._list.dataProvider = this.getDataProvider(1);
            }
            else if (event.currentTarget == this._btnSearch)
            {
                this._list.dataProvider = this.getDataProvider(2);
            }
            else if (event.currentTarget == this._btnReset)
            {
                this._comboLevel.selectedIndex = 0;
                this._comboColor.selectedIndex = 0;
                this._comboCareer.selectedIndex = 0;
                this._comboType1.selectedIndex = 0;
                this.resetComboType2();
            }
            return;
        }// end function

        public function get searchCondition() : Object
        {
            if (this._searchCondition == null)
            {
                this._searchCondition = {};
            }
            if (this._comboColor.selectedItem.type != 0)
            {
                this._searchCondition.color = this._comboColor.selectedItem.color;
            }
            else
            {
                delete this._searchCondition.color;
            }
            if (this._comboCareer.selectedItem.type != 0)
            {
                this._searchCondition.career = this._comboCareer.selectedItem.career;
            }
            else
            {
                delete this._searchCondition.career;
            }
            if (this._comboLevel.selectedItem.type != 0)
            {
                this._searchCondition.minLevel = this._comboLevel.selectedItem.min;
                this._searchCondition.maxLevel = this._comboLevel.selectedItem.max;
            }
            else
            {
                delete this._searchCondition.minLevel;
                delete this._searchCondition.maxLevel;
            }
            var _loc_1:* = this._comboType2.selectedItem.tMarket;
            this._searchCondition.typeArr = MarketConfig.decodeItemType(_loc_1);
            return this._searchCondition;
        }// end function

        private function onSearchTxtChange() : void
        {
            if (this._txtSearch._textInput.text.length >= 2)
            {
                this._txtSearch.updateDataProvider(this.getTxtListDp());
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._selectItemInfo = null;
            this._bgTitle1.dispose(param1);
            this._title1.dispose(param1);
            this._selectSp.dispose(param1);
            this._list.dispose(param1);
            this._pageSelect.dispose(param1);
            this._bgTitle2.dispose(param1);
            this._title2.dispose(param1);
            this._line.dispose(param1);
            this._txtSearch.dispose(param1);
            this._btnMagnifier.dispose(param1);
            this._comboType1.dispose(param1);
            this._comboColor.dispose(param1);
            this._comboType2.dispose(param1);
            this._comboCareer.dispose(param1);
            this._comboLevel.dispose(param1);
            this._btnSearch.dispose(param1);
            this._btnReset.dispose(param1);
            this._moneys.dispose(param1);
            this._bgTitle1 = null;
            this._title1 = null;
            this._selectSp = null;
            this._list = null;
            this._pageSelect = null;
            this._bgTitle2 = null;
            this._title2 = null;
            this._line = null;
            this._txtSearch = null;
            this._btnMagnifier = null;
            this._txtType1 = null;
            this._comboType1 = null;
            this._comboColor = null;
            this._comboType2 = null;
            this._comboCareer = null;
            this._comboLevel = null;
            this._btnSearch = null;
            this._btnReset = null;
            this._moneys = null;
            return;
        }// end function

        private function onPageChange(event:Event) : void
        {
            var _loc_2:ItemInfo = null;
            var _loc_3:MktBagItemRenderer = null;
            var _loc_4:int = 0;
            this._list.dataProvider = this.getDataProvider(0);
            this._list.drawNow();
            this._list.selectedIndex = -1;
            if (this._selectItemInfo && this._pageSelect.currentPage == this._selectPage)
            {
                _loc_4 = 0;
                while (_loc_4 < this._list.dataProvider.length)
                {
                    
                    _loc_2 = this._list.getItemAt(_loc_4) as ItemInfo;
                    if (_loc_2 && _loc_2 === this._selectItemInfo)
                    {
                        this._list.selectedIndex = _loc_4;
                        break;
                    }
                    _loc_4++;
                }
            }
            _loc_3 = this._list.itemToCellRenderer(_loc_2) as MktBagItemRenderer;
            this.setSelectSpPos(_loc_3);
            return;
        }// end function

        private function onComboboxChange(event:Event) : void
        {
            var _loc_2:Object = null;
            if (event.currentTarget == this._comboType1)
            {
                _loc_2 = this._comboType1.selectedItem;
                this.resetComboType2();
            }
            this._list.dataProvider = this.getDataProvider(2);
            return;
        }// end function

        private function resetComboType2() : void
        {
            this._comboType2.dataProvider = MktModConfig.getdpItemType2(this._comboType1.selectedItem.type);
            this._comboType2.textField.setStyle("textFormat", GlobalStyle.textFormatPutong.center());
            this._comboType2.textField.drawNow();
            this._comboType2.selectedIndex = 0;
            return;
        }// end function

        private function getDataProvider(param1:int) : DataProvider
        {
            var _loc_3:Array = null;
            var _loc_2:* = new DataProvider();
            if (param1 == 0)
            {
                _loc_3 = this._itemArr;
            }
            else if (param1 == 1)
            {
                _loc_3 = MarketCache.searchByName(this._txtSearch._textInput.text);
                this._itemArr = _loc_3;
            }
            else if (param1 == 2)
            {
                _loc_3 = MarketCache.searchByCondition(this.searchCondition);
                this._itemArr = _loc_3;
            }
            var _loc_4:* = 2 * this._list.columnCount;
            var _loc_5:* = (this._pageSelect.currentPage - 1) * _loc_4;
            var _loc_6:* = Math.max(_loc_3.length, _loc_4);
            var _loc_7:* = _loc_5;
            while (_loc_7 < _loc_6)
            {
                
                if (_loc_3 && _loc_3[_loc_7])
                {
                    _loc_2.addItem(_loc_3[_loc_7]);
                }
                else
                {
                    _loc_2.addItem(new Object());
                }
                _loc_7++;
            }
            if (_loc_3.length == 0)
            {
            }
            this._pageSelect.maxPage = Math.ceil(_loc_3.length / _loc_4);
            this._pageSelect.currentPage = Math.ceil((_loc_5 + 1) / _loc_4);
            return _loc_2;
        }// end function

        private function getTxtListDp() : DataProvider
        {
            var _loc_1:* = MarketCache.searchByName(this._txtSearch._textInput.text);
            if (_loc_1.length == 0)
            {
            }
            return new DataProvider(_loc_1);
        }// end function

    }
}
